package com.maimao.auth.imageCode.email;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @author MaoLin Wang
 * @date 2020/12/14 3:23 下午
 */
public class EmailPwdCodeAuthenticationToken extends AbstractAuthenticationToken {

    private Object principal;
    /**
     * 图片验证码
     */
    private String code;

    private Object credentials;
    private String ip;


    public EmailPwdCodeAuthenticationToken(String email, String password, String code,String ip) {
        super(null);
        this.principal = email;
        this.ip = ip;
        this.code = code;
        this.credentials=password;
        //第一次未认证
        this.setAuthenticated(false);
    }

    /**
     * 认证通过后走这个构造
     * @param principal 认证后的对象
     * @param authorities 认证后查询到的权限
     */
    public EmailPwdCodeAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal=principal;
        this.credentials=credentials;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }
    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    public String getCode() {
        return code;
    }

    public String getIp() {
        return ip;
    }
}

